package com.fr.report.web.ui;

import com.fr.base.BaseUtils;
import com.fr.base.StringUtils;
import com.fr.base.XMLable;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.util.ListMap;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.web.Browser;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class IconManager
  implements XMLable
{
  public static final String XML_TAG = "IM";
  private static ListMap iconMap = new ListMap();
  private static List systemIconList = new ArrayList();
  public static final Icon SUBMIT = getDefaultIcon("submit", "/com/fr/web/images/save.png");
  public static final Icon SUBMITFORCILY = getDefaultIcon("submitforcily", "/com/fr/web/images/save2.png");
  public static final Icon FIRST = getDefaultIcon("first", "/com/fr/web/images/first.png");
  public static final Icon PREVIOUS = getDefaultIcon("previous", "/com/fr/web/images/previous.png");
  public static final Icon NEXT = getDefaultIcon("next", "/com/fr/web/images/next.png");
  public static final Icon LAST = getDefaultIcon("last", "/com/fr/web/images/last.png");
  public static final Icon RELOAD = getDefaultIcon("reload", "/com/fr/web/images/reload.png");
  public static final Icon PAGESETUP = getDefaultIcon("pagesetup", "/com/fr/web/images/pageSetup.png");
  public static final Icon FLASHPRINT = getDefaultIcon("flashprint", "/com/fr/web/images/flashPrint.png");
  public static final Icon PDFPRINT = getDefaultIcon("pdfprint", "/com/fr/web/images/pdfPrint.png");
  public static final Icon APPLETPRINT = getDefaultIcon("appletprint", "/com/fr/web/images/appletPrint.png");
  public static final Icon PDF = getDefaultIcon("pdf", "/com/fr/web/images/pdf.png");
  public static final Icon EXCEL = getDefaultIcon("excel", "/com/fr/web/images/excel.png");
  public static final Icon WORD = getDefaultIcon("word", "/com/fr/web/images/word.png");
  public static final Icon PRINT = getDefaultIcon("print", "/com/fr/web/images/print.png");
  public static final Icon EXPORT = getDefaultIcon("export", "/com/fr/web/images/export.png");
  public static final Icon EMAIL = getDefaultIcon("email", "/com/fr/web/images/email.png");
  public static final Icon FROZEN = getDefaultIcon("frozzen", "/com/fr/web/images/frozen.png");
  public static final Icon DEFAULT = getDefaultIcon("default", "/com/fr/web/images/default.png");
  public static final Icon DELETECOLUMN = getDefaultIcon("deletecolumn", "/com/fr/web/images/deleteColumn.png");
  public static final Icon DELETEROW = getDefaultIcon("deleterow", "/com/fr/web/images/deleteRow.png");
  public static final Icon APPENDCOLUMN = getDefaultIcon("appendcolumn", "/com/fr/web/images/appendColumn.png");
  public static final Icon APPENDROW = getDefaultIcon("appendrow", "/com/fr/web/images/appendRow.png");
  public static final Icon VERIFY = getDefaultIcon("verify", "/com/fr/web/images/verify.gif");
  public static final Icon ADD = getDefaultIcon("add", "/com/fr/web/images/add.png");
  public static final Icon DELETE = getDefaultIcon("delete", "/com/fr/web/images/delete.png");
  public static final Icon OK = getDefaultIcon("ok", "/com/fr/web/images/ok.png");
  public static final Icon PREVIEW = getDefaultIcon("preview", "/com/fr/web/images/preview.png");
  public static final Icon EDIT = getDefaultIcon("edit", "/com/fr/web/images/edit.png");
  private static final String ICONCLASSNAME = "x-emb-";
  private IconInfo[] iis = null;
  private byte[] allInOneImageBytes = null;
  private Map iconImageBytesCacheMap = new HashMap();
  private StringBuffer iconCss = new StringBuffer();
  private StringBuffer iconCss2 = new StringBuffer();
  private int mark4Init = 0;
  public static final int DEFAULT_ICONWIDTH = 16;
  public static final int DEFAULT_ICONHEIGHT = 16;
  public static final String ICONRESOURCE = "icon_name_";
  private static final int spacing = 2;

  private static Icon getDefaultIcon(String paramString1, String paramString2)
  {
    paramString1 = paramString1.toLowerCase();
    Icon localIcon = new Icon(paramString1, paramString2);
    iconMap.put(paramString1, localIcon);
    systemIconList.add(paramString1);
    return localIcon;
  }

  private void init4Image()
  {
    label326: Rectangle localRectangle;
    Object localObject1 = null;
    int i = 0;
    int j = 0;
    int k = 0;
    int l = 0;
    this.iis = new IconInfo[iconMap.size()];
    ImageInfo[] arrayOfImageInfo = new ImageInfo[iconMap.size()];
    for (int i1 = 0; i1 < iconMap.size(); ++i1)
    {
      Icon localIcon = (Icon)iconMap.getByIndex(i1);
      String str1 = localIcon.getPath();
      String str2 = localIcon.getName();
      if (StringUtils.isBlank(str2))
        break label326:
      if (isSystemIcon(str2))
      {
        if ((!(str1.startsWith("/"))) && (!(str1.startsWith("\\"))))
          str1 = File.separator + str1;
        localObject1 = BaseUtils.readImage(str1);
        localIcon.setImage((Image)localObject1);
      }
      else
      {
        localObject1 = localIcon.getImage();
        if (localObject1 != null)
          str1 = "icon_name_" + str2;
      }
      this.iis[i1] = new IconInfo(str2, "icon_name_" + str2, null);
      if (localObject1 != null)
      {
        k = ((Image)localObject1).getWidth(null);
        k = (k > -1) ? k : 16;
        l = ((Image)localObject1).getHeight(null);
        l = (l > -1) ? l : 16;
        localObject2 = new Rectangle(0, j, k, l);
        j += l + 2;
        i = (i > k) ? i : k;
        arrayOfImageInfo[i1] = new ImageInfo((Image)localObject1, (Rectangle)localObject2);
      }
      else
      {
        arrayOfImageInfo[i1] = null;
      }
    }
    BufferedImage localBufferedImage = GraphHelper.createBufferedImage(i, j);
    Object localObject2 = localBufferedImage.createGraphics();
    for (int i2 = 0; i2 < this.iis.length; ++i2)
    {
      if (arrayOfImageInfo[i2] == null)
        break label430:
      localRectangle = arrayOfImageInfo[i2].getRectangle();
      localObject1 = arrayOfImageInfo[i2].getImage();
      Shape localShape = ((Graphics2D)localObject2).getClip();
      ((Graphics2D)localObject2).clip(localRectangle);
      ((Graphics2D)localObject2).drawImage((Image)localObject1, localRectangle.x, localRectangle.y, null);
      ((Graphics2D)localObject2).setClip(localShape);
    }
    label430: localBufferedImage.flush();
    ((Graphics2D)localObject2).dispose();
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    BaseUtils.writeImage(localBufferedImage, "png", localByteArrayOutputStream);
    this.allInOneImageBytes = localByteArrayOutputStream.toByteArray();
    for (int i3 = 0; i3 < this.iis.length; ++i3)
      if (arrayOfImageInfo[i3] != null)
      {
        if (this.iis[i3] == null)
          break label568:
        localRectangle = arrayOfImageInfo[i3].getRectangle();
        this.iis[i3].setCss(localRectangle.x + "px " + "-" + localRectangle.y + "px");
      }
    label568:
  }

  private void init4Css(String paramString)
  {
    for (int i = 0; i < this.iis.length; ++i)
      if ((this.iis[i] != null) && (this.iis[i].getCss() != null))
      {
        this.iconCss.append(".").append("x-emb-").append(this.iis[i].getName()).append("{background:").append("url(").append(getAllInOneImageUrl(paramString)).append(") ").append("no-repeat ").append(this.iis[i].getCss()).append(";}\n");
        this.iconCss2.append(".").append("x-emb-").append(this.iis[i].getName()).append("{\n").append("filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='").append(getAllInOneImagePath(paramString, this.iis[i].getPath())).append("');\n}\n");
      }
  }

  public String getCssInfo(String paramString1, String paramString2)
  {
    int i;
    if (paramString1 == null)
      return "";
    paramString1 = paramString1.toLowerCase();
    checkInit(paramString2);
    if (this.iis != null)
      for (i = 0; i < this.iis.length; ++i)
        if ((this.iis[i] != null) && (this.iis[i].getName() != null) && (this.iis[i].getName().equals(paramString1)))
          return "css:" + "x-emb-" + this.iis[i].getName();
    return "";
  }

  private void checkInit(String paramString)
  {
    if (this.mark4Init <= 0)
    {
      init4Image();
      if (StringUtils.isNotBlank(paramString))
      {
        init4Css(paramString);
        this.mark4Init = 1;
      }
    }
  }

  public String getCssFile(String paramString, Browser paramBrowser)
  {
    checkInit(paramString);
    if (paramBrowser.supportTransparentImage())
      return this.iconCss.toString();
    return this.iconCss2.toString();
  }

  public byte[] getAllInOneImageBytes(String paramString)
  {
    checkInit(paramString);
    if (this.allInOneImageBytes != null)
      return this.allInOneImageBytes;
    return new byte[0];
  }

  public byte[] getIconImage4Web(String paramString1, String paramString2)
  {
    checkInit(paramString1);
    String str = paramString2.substring("icon_name_".length());
    if (str == null)
      return new byte[0];
    str = str.toLowerCase();
    if (this.iconImageBytesCacheMap.containsKey(str))
      return ((byte[])this.iconImageBytesCacheMap.get(str));
    Image localImage = getIconImage(str);
    if (localImage != null)
    {
      ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
      BaseUtils.writeImage(localImage, "png", localByteArrayOutputStream);
      byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
      this.iconImageBytesCacheMap.put(str, arrayOfByte);
      return arrayOfByte;
    }
    return new byte[0];
  }

  private String getAllInOneImageUrl(String paramString)
  {
    return paramString + "?op=attach_image&id=toolbar-image.png";
  }

  private String getAllInOneImagePath(String paramString1, String paramString2)
  {
    return paramString1 + "?op=attach_image&id=" + paramString2;
  }

  public boolean contains(String paramString)
  {
    if (paramString == null)
      return false;
    paramString = paramString.toLowerCase();
    return iconMap.containsKey(paramString);
  }

  public boolean addIcon(Icon paramIcon, boolean paramBoolean)
  {
    if ((paramIcon == null) || (StringUtils.isBlank(paramIcon.getName())) || (paramIcon.getImage() == null))
      return false;
    String str = paramIcon.getName().toLowerCase();
    paramIcon.setName(str);
    if ((iconMap.containsKey(str)) && (!(paramBoolean)))
      return false;
    iconMap.put(str, paramIcon);
    this.mark4Init = 0;
    return true;
  }

  public boolean removeIcon(String paramString)
  {
    if (StringUtils.isBlank(paramString))
      return false;
    paramString = paramString.toLowerCase();
    if (iconMap.remove(paramString) != null)
    {
      this.mark4Init = 0;
      return true;
    }
    return false;
  }

  public int getIconCount()
  {
    return iconMap.size();
  }

  public boolean isSystemIcon(String paramString)
  {
    if (paramString == null)
      return false;
    paramString = paramString.toLowerCase();
    return systemIconList.contains(paramString);
  }

  public Image getIconImage(String paramString)
  {
    Object localObject;
    if (paramString == null)
      return null;
    paramString = paramString.toLowerCase();
    Icon localIcon = getIcon4Self(paramString);
    if (localIcon == null)
      return null;
    if (localIcon.getImage() != null)
    {
      localObject = localIcon.getImage();
    }
    else
    {
      String str = localIcon.getPath();
      if (StringUtils.isBlank(str))
        return null;
      if ((!(str.startsWith("/"))) && (!(str.startsWith("\\"))))
        str = File.separator + str;
      localObject = BaseUtils.readImage(str);
      localIcon.setImage((Image)localObject);
    }
    return ((Image)localObject);
  }

  public Icon getIcon(Object paramObject)
    throws CloneNotSupportedException
  {
    if ((paramObject == null) || (!(paramObject instanceof String)))
      return null;
    String str = ((String)paramObject).toLowerCase();
    return ((Icon)((Icon)iconMap.get(str)).clone());
  }

  private Icon getIcon4Self(Object paramObject)
  {
    if ((paramObject == null) || (!(paramObject instanceof String)))
      return null;
    String str = ((String)paramObject).toLowerCase();
    return ((Icon)iconMap.get(str));
  }

  public Object[] getIconNames()
  {
    return iconMap.keySet().toArray();
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      String str = paramXMLableReader.getTagName();
      Icon localIcon = new Icon();
      if (Icon.XML_TAG.equals(str))
        paramXMLableReader.readXMLObject(localIcon);
      addIcon(localIcon, false);
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    int i;
    paramXMLPrintWriter.startTAG("IM");
    if ((iconMap != null) && (iconMap.size() > 0))
      for (i = 0; i < iconMap.size(); ++i)
      {
        Icon localIcon = (Icon)iconMap.getByIndex(i);
        if (isSystemIcon(localIcon.getName()))
          break label64:
        localIcon.writeXML(paramXMLPrintWriter);
      }
    label64: paramXMLPrintWriter.end();
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    IconManager localIconManager = (IconManager)super.clone();
    return localIconManager;
  }

  private static class ImageInfo
  {
    private Image image;
    private Rectangle rectangle;

    public ImageInfo(Image paramImage, Rectangle paramRectangle)
    {
      setImage(paramImage);
      setRectangle(paramRectangle);
    }

    public Image getImage()
    {
      return this.image;
    }

    public void setImage(Image paramImage)
    {
      this.image = paramImage;
    }

    public Rectangle getRectangle()
    {
      return this.rectangle;
    }

    public void setRectangle(Rectangle paramRectangle)
    {
      this.rectangle = paramRectangle;
    }
  }

  private static class IconInfo
  {
    private String name;
    private String path;
    private String css;

    public IconInfo(String paramString1, String paramString2, String paramString3)
    {
      setName(paramString1);
      setPath(paramString2);
      setCss(paramString3);
    }

    public String getCss()
    {
      return this.css;
    }

    public void setCss(String paramString)
    {
      this.css = paramString;
    }

    public String getName()
    {
      return this.name;
    }

    public void setName(String paramString)
    {
      this.name = paramString;
    }

    public String getPath()
    {
      return this.path;
    }

    public void setPath(String paramString)
    {
      this.path = paramString;
    }
  }
}